import { EArenaFinalPosType } from "../../../../../Message/Public/EArenaFinalPosType";
import { FinalPlayerCell } from "./cell/FinalPlayerCell";
import { ObjCreate } from "../../common/ObjCreate";
import { Language } from "../../../../../extend/language/Language";
import { FinalMatchTree } from "./tree/FinalMatchTree";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { LoaderHelp } from "../../../../common/display/LoaderHelp";
import { ResFileConst } from "../../../resource/ResFileConst";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../../resource/ImagesConst";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { GameManager } from "../../../manager/GameManager";
import { ArenaCrossFinalMyBattlesWindows } from "./ArenaCrossFinalMyBattlesWindows";
import { ArenaCrossFinalMyBetRankWindow } from "./ArenaCrossFinalMyBetRankWindow";
import { ArenaCrossFinalStatusRankWindow } from "./ArenaCrossFinalStatusRankWindow";
import { SArenaCrossFinalBattles } from "../../../../../Message/Public/SArenaCrossFinalBattles";
import { SArenaCrossFinalBattle } from "../../../../../Message/Public/SArenaCrossFinalBattle";
import { AvatarInfo } from "../../../resource/info/AvatarInfo";
import { AvatarType } from "../../../resource/info/AvatarType";
import { ImageInfo } from "../../../../../com/gengine/resource/info/ImageInfo";
import { PlayerUtil } from "../../common/util/PlayerUtil";
import { Cache } from "../../../cache/Cache";
import { EArenaProcess } from "../../../../../Message/Public/EArenaProcess";
import { SArenaCrossGroupBattleEnd } from "../../../../../Message/Public/SArenaCrossGroupBattleEnd";
import { GButton } from "../../../../../com/mui/controls/GButton";
type int = number;
//class FinalMatchPanel
    
    export  class FinalMatchPanel /* flash.display.Sprite */
    {
        constructor()
        {
            
            this._aryPlayerCell = [];
            this.init();
            this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddToStage);
            return;
        }

        protected onClickAir(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._selectType != EArenaFinalPosType._EArenaFinalPosTop) 
            {
                this._selectType = EArenaFinalPosType._EArenaFinalPosTop;
                this.getData();
                this.updateBmp();
            }
            return;
        }

        protected onAddToStage(arg1: Object /* flash.events.Event */): void
        {
            this.getData();
            return;
        }

        protected createPlayerCell(arg1: int, arg2: int): FinalPlayerCell
        {
            var loc1=new FinalPlayerCell();
            loc1.x = arg1;
            loc1.y = arg2;
            this.addChild(loc1);
            return loc1;
        }

        protected init(): void
        {
            var loc3=0;
            var loc4=null;
            var loc1=0;
            while (loc1 < 32) 
            {
                loc3 = loc1 >= 16 ? 674 : 8;
                loc4 = this.createPlayerCell(loc3, 18 + loc1 % 16 * 30);
                this._aryPlayerCell.push(loc4);
                ++loc1;
            }
            this._btnMatchRule = ObjCreate.createGButton(Language.getString(44341), 249, 30, 68, 25, this, "ArenaRedBtn");
            this._btnMyMatch = ObjCreate.createGButton(Language.getString(44342), 329, 30, 68, 25, this, "ArenaRedBtn");
            this._btnMyBet = ObjCreate.createGButton(Language.getString(44343), 409, 30, 68, 25, this, "ArenaRedBtn");
            this._btnWorthRank = ObjCreate.createGButton(Language.getString(44344), 489, 30, 68, 25, this, "ArenaRedBtn");
            this._bmpBg = new Object /* flash.display.Bitmap */();
            this._bmpBg.x = 215;
            this._bmpBg.y = 89;
            this.addChild(this._bmpBg);
            this._bmpDoubt = new Object /* flash.display.Bitmap */();
            this._bmpDoubt.x = 386;
            this._bmpDoubt.y = 226;
            this.addChild(this._bmpDoubt);
            this._bmpKemp = new Object /* flash.display.Bitmap */();
            this._bmpKemp.x = 335;
            this._bmpKemp.y = 177;
            this.addChild(this._bmpKemp);
            this._spAir = new Object /* flash.display.Sprite */();
            this._spAir.mouseEnabled = true;
            this._spAir.x = 268;
            this._spAir.y = 77;
            this._spAir.buttonMode = true;
            this.addChild(this._spAir);
            this._bmpBgAir = new Object /* flash.display.Bitmap */();
            this._spAir.addChild(this._bmpBgAir);
            this._bmpAir = new Object /* flash.display.Bitmap */();
            this._bmpAir.x = 6;
            this._bmpAir.y = 6;
            this._spAir.addChild(this._bmpAir);
            this._spEarth = new Object /* flash.display.Sprite */();
            this._spEarth.x = 420;
            this._spEarth.y = 77;
            this._spEarth.buttonMode = true;
            this._spEarth.mouseEnabled = true;
            this.addChild(this._spEarth);
            this._bmpBgEarth = new Object /* flash.display.Bitmap */();
            this._spEarth.addChild(this._bmpBgEarth);
            this._bmpEarth = new Object /* flash.display.Bitmap */();
            this._bmpEarth.x = 6;
            this._bmpEarth.y = 6;
            this._spEarth.addChild(this._bmpEarth);
            this._finalTree = new FinalMatchTree();
            this._finalTree.x = 128;
            this._finalTree.y = 28;
            this.addChild(this._finalTree);
            this._winnderHeadSp = new Object /* flash.display.Sprite */();
            this._winnderHeadSp.x = 370;
            this._winnderHeadSp.y = 221;
            this._winnderHeadSp.buttonMode = true;
            this.addChild(this._winnderHeadSp);
            this._winnerHead = new Object /* flash.display.Bitmap */();
            this._winnerHead.x = 0;
            this._winnerHead.y = 0;
            this._winnderHeadSp.addChild(this._winnerHead);
            var loc2=GlobalStyle.textFormatOrange;
            loc2.align = flash.text.TextFormatAlign.CENTER;
            this._tfWinnerHead = ObjCreate.createTextField("", 320, 300, 170, 20, this, loc2);
            this._tfWinnerHead.mouseEnabled = false;
            this._textTimeDesc1 = ObjCreate.createTextField("", 313, 370, 180, 120, this);
            this._textTimeDesc1.htmlText = Language.getString(44345);
            this._textTimeDesc2 = ObjCreate.createTextField("", 313, 390, 180, 120, this);
            this._textTimeDesc2.htmlText = Language.getString(44346);
            this._textTimeDesc3 = ObjCreate.createTextField("", 313, 410, 180, 120, this);
            this._textTimeDesc3.htmlText = Language.getString(44347);
            this._textTimeDesc4 = ObjCreate.createTextField("", 313, 430, 180, 120, this);
            this._textTimeDesc4.htmlText = Language.getString(44348);
            this._textTimeDesc5 = ObjCreate.createTextField("", 313, 450, 180, 120, this);
            this._textTimeDesc5.htmlText = Language.getString(44349);
            LoaderHelp.addResCallBack(ResFileConst.ArenaCrossFinalSelectPos, this.onResCompl);
            LoaderHelp.addResCallBack(ResFileConst.ArenaCrossFinalMatch, this.onRes2Compl);
            this.addListeners();
            return;
        }

        protected onResCompl(): void
        {
            this._bmpBg.bitmapData = GlobalClass.getBitmapData(ImagesConst.finalSelectPosbg);
            this._bmpDoubt.bitmapData = GlobalClass.getBitmapData(ImagesConst.doubt);
            this._bmpKemp.bitmapData = GlobalClass.getBitmapData(ImagesConst.airKemp);
            this._isResCompl = true;
            this.updateBmp();
            return;
        }

        protected onRes2Compl(): void
        {
            this._isRes2Compl = true;
            this._bmpAir.bitmapData = GlobalClass.getBitmapData(ImagesConst.FinalMatchLookAirTop);
            this._bmpEarth.bitmapData = GlobalClass.getBitmapData(ImagesConst.FinalMatchLookEarthMatch);
            this._bmpBgAir.bitmapData = GlobalClass.getBitmapData(ImagesConst.FinalMatchLookBg_overSkin);
            this._bmpBgEarth.bitmapData = GlobalClass.getBitmapData(ImagesConst.FinalMatchLookBg_upSkin);
            this.updateBmp();
            return;
        }

        protected updateBmp(): void
        {
            if (this._isRes2Compl && this._isResCompl) 
            {
                if (this._selectType != EArenaFinalPosType._EArenaFinalPosTop) 
                {
                    this._bmpBgAir.bitmapData = GlobalClass.getBitmapData(ImagesConst.FinalMatchLookBg_upSkin);
                    this._bmpBgEarth.bitmapData = GlobalClass.getBitmapData(ImagesConst.FinalMatchLookBg_overSkin);
                    this._bmpKemp.bitmapData = GlobalClass.getBitmapData(ImagesConst.earthKemp);
                }
                else 
                {
                    this._bmpBgAir.bitmapData = GlobalClass.getBitmapData(ImagesConst.FinalMatchLookBg_overSkin);
                    this._bmpBgEarth.bitmapData = GlobalClass.getBitmapData(ImagesConst.FinalMatchLookBg_upSkin);
                    this._bmpKemp.bitmapData = GlobalClass.getBitmapData(ImagesConst.airKemp);
                }
            }
            return;
        }

        protected addListeners(): void
        {
            this._btnMatchRule.addEventListener(flash.events.MouseEvent.CLICK, this.onClickMatchRule);
            this._btnMyMatch.addEventListener(flash.events.MouseEvent.CLICK, this.onClickMyMatch);
            this._btnMyBet.addEventListener(flash.events.MouseEvent.CLICK, this.onClickMyBet);
            this._btnWorthRank.addEventListener(flash.events.MouseEvent.CLICK, this.onClickWorthBank);
            this._spAir.addEventListener(flash.events.MouseEvent.CLICK, this.onClickAir);
            this._spEarth.addEventListener(flash.events.MouseEvent.CLICK, this.onClickEarth);
            this._winnderHeadSp.addEventListener(flash.events.MouseEvent.CLICK, this.onClickHeadSp);
            return;
        }

        protected onClickMatchRule(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.KingWarRuleDescToTabBar, 2));
            return;
        }

        protected onClickMyMatch(arg1: Object /* flash.events.MouseEvent */): void
        {
            GameManager.instance.popupView(ArenaCrossFinalMyBattlesWindows.instance);
            return;
        }

        protected onClickMyBet(arg1: Object /* flash.events.MouseEvent */): void
        {
            GameManager.instance.popupView(ArenaCrossFinalMyBetRankWindow.instance);
            return;
        }

        protected onClickWorthBank(arg1: Object /* flash.events.MouseEvent */): void
        {
            GameManager.instance.popupView(ArenaCrossFinalStatusRankWindow.instance);
            return;
        }

        protected onClickEarth(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._selectType != EArenaFinalPosType._EArenaFinalPosDown) 
            {
                this._selectType = EArenaFinalPosType._EArenaFinalPosDown;
                this.getData();
                this.updateBmp();
            }
            return;
        }

        protected onClickHeadSp(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._finalWinner) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ArenaCrossGetArenaCrossPlayerInfo, this._finalWinner.entityId));
            }
            return;
        }

        protected getData(): void
        {
            var loc1=this._selectType;
            if (!this._isHasUpdateData) 
            {
                loc1 = 0;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.ArenaCrossGetFinalMatchData, loc1));
            return;
        }

        public updateFinalBattles(arg1: SArenaCrossFinalBattles): void
        {
            var finalBattles: SArenaCrossFinalBattles;
            var finalPlayerCell: FinalPlayerCell;
            var i: int;
            var isFinalOver: Boolean;
            var playerBattle: SArenaCrossFinalBattle;
            var pos: int;

            var loc1;
            finalPlayerCell = null;
            i = 0;
            playerBattle = null;
            pos = 0;
            finalBattles = arg1;
            if (!(finalBattles.finalPosType == this._selectType) && this._isHasUpdateData) 
            {
                return;
            }
            this._isHasUpdateData = true;
            this._finalWinner = null;
            this._selectType = finalBattles.finalPosType;
            this.updateBmp();
            i = 0;
            while (i < this._aryPlayerCell.length) 
            {
                finalPlayerCell = this._aryPlayerCell[i] as FinalPlayerCell;
                finalPlayerCell.updateData(null);
                ++i;
            }
            i = 0;
            while (i < finalBattles.finalBattles.length) 
            {
                playerBattle = finalBattles.finalBattles[i];
                if (playerBattle.positions.length) 
                {
                    pos = playerBattle.positions[0];
                    if (pos <= this._aryPlayerCell.length) 
                    {
                        finalPlayerCell = this._aryPlayerCell[(pos - 1)];
                        finalPlayerCell.updateData(playerBattle);
                    }
                }
                if (playerBattle.positions.length && playerBattle.positions[(playerBattle.positions.length - 1)] == 63) 
                {
                    this._finalWinner = playerBattle;
                }
                ++i;
            }
            this._finalTree.updateFinalBattles(finalBattles);
            if (this._finalWinner) 
            {
                AvatarInfo.getAvatar(this._finalWinner.camp, this._finalWinner.sex, AvatarType.BIG, (arg1: ImageInfo): void
                {
                    _winnerHead.bitmapData = arg1.bitmapData;
                    return;
                }, this._finalWinner.avatarId)
                this._tfWinnerHead.text = PlayerUtil.getProxyAndNameByEntityIdAndName(this._finalWinner.entityId, this._finalWinner.name);
            }
            isFinalOver = Cache.instance.arenaCross.arenaCrossInfo && Cache.instance.arenaCross.arenaCrossInfo.process == EArenaProcess._EArenaFinalMatch2 && Cache.instance.arenaCross.arenaCrossInfo.isOpen == false && !Cache.instance.arenaCross.isWillOpen;
            if (finalBattles.ring > 1 || finalBattles.ring == 1 && finalBattles.battleNumInRing == 0 || isFinalOver) 
            {
                this._textTimeDesc1.htmlText = Language.getString(44350);
            }
            if (finalBattles.ring > 2 || finalBattles.ring == 2 && finalBattles.battleNumInRing == 0 || isFinalOver) 
            {
                this._textTimeDesc2.htmlText = Language.getString(44351);
            }
            if (finalBattles.ring > 3 || finalBattles.ring == 3 && finalBattles.battleNumInRing == 0 || isFinalOver) 
            {
                this._textTimeDesc3.htmlText = Language.getString(44352);
            }
            if (finalBattles.ring > 4 || finalBattles.ring == 4 && finalBattles.battleNumInRing == 0 || isFinalOver) 
            {
                this._textTimeDesc4.htmlText = Language.getString(44353);
            }
            if (finalBattles.ring > 5 || finalBattles.ring == 5 && finalBattles.battleNumInRing == 0 || isFinalOver) 
            {
                this._textTimeDesc5.htmlText = Language.getString(44354);
            }
            return;
        }

        public updateBattleEnd(arg1: SArenaCrossGroupBattleEnd): void
        {
            this._finalTree.updateBattleEnd(arg1);
            return;
        }

        protected /* var */_bmpKemp: Object /* flash.display.Bitmap */;

        protected /* var */_textTimeDesc1: Object /* flash.text.TextField */;

        protected /* var */_textTimeDesc2: Object /* flash.text.TextField */;

        protected /* var */_textTimeDesc3: Object /* flash.text.TextField */;

        protected /* var */_textTimeDesc4: Object /* flash.text.TextField */;

        protected /* var */_btnWorthRank: GButton;

        protected /* var */_myPos: int=0;

        protected /* var */_isResCompl: Boolean=false;

        protected /* var */_isRes2Compl: Boolean=false;

        protected /* var */_selectType: int=1;

        protected /* var */_isHasUpdateData: Boolean=false;

        protected /* var */_finalWinner: SArenaCrossFinalBattle;

        protected /* var */_aryPlayerCell: Array<any>;

        protected /* var */_btnMatchRule: GButton;

        protected /* var */_btnMyMatch: GButton;

        protected /* var */_btnMyBet: GButton;

        protected /* var */_spAir: Object /* flash.display.Sprite */;

        protected /* var */_bmpBgAir: Object /* flash.display.Bitmap */;

        protected /* var */_bmpAir: Object /* flash.display.Bitmap */;

        protected /* var */_spEarth: Object /* flash.display.Sprite */;

        protected /* var */_bmpBgEarth: Object /* flash.display.Bitmap */;

        protected /* var */_bmpEarth: Object /* flash.display.Bitmap */;

        protected /* var */_finalTree: FinalMatchTree;

        protected /* var */_textTimeDesc5: Object /* flash.text.TextField */;

        protected /* var */_bmpBg: Object /* flash.display.Bitmap */;

        protected /* var */_bmpDoubt: Object /* flash.display.Bitmap */;

        protected /* var */_winnderHeadSp: Object /* flash.display.Sprite */;

        protected /* var */_winnerHead: Object /* flash.display.Bitmap */;

        protected /* var */_tfWinnerHead: Object /* flash.text.TextField */;
    }
